package com.bootdo.lvyou_specialty.service.impl;

import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.lvyou_specialty.dao.LvyouSpecialtyLunboDao;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyLunbo;
import com.bootdo.lvyou_specialty.service.LvyouSpecialtyLunboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.bootdo.lvyou_specialty.dao.LvyouSpecialtyProductDao;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyProduct;
import com.bootdo.lvyou_specialty.service.LvyouSpecialtyProductService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class LvyouSpecialtyProductServiceImpl implements LvyouSpecialtyProductService {
	@Autowired
	private LvyouSpecialtyProductDao lvyouSpecialtyProductDao;

	@Autowired
	private LvyouSpecialtyLunboDao lvyouSpecialtyLunboDao;
	
	@Override
	public LvyouSpecialtyProduct get(Long id){
		return lvyouSpecialtyProductDao.get(id);
	}
	
	@Override
	public List<LvyouSpecialtyProduct> list(Map<String, Object> map){
		return lvyouSpecialtyProductDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return lvyouSpecialtyProductDao.count(map);
	}
	
	@Override
	public int save(LvyouSpecialtyProduct lvyouSpecialtyProduct){
	
		if(lvyouSpecialtyProduct.getId() != null) {
			return lvyouSpecialtyProductDao.update(lvyouSpecialtyProduct);
		}else {
			if (lvyouSpecialtyProduct.getPendingFlag() == null) {
				lvyouSpecialtyProduct.setPendingFlag(1);
			} else {
				lvyouSpecialtyProduct.setMerchantName("旅游局");
				lvyouSpecialtyProduct.setMerchantId(ShiroUtils.getUserId());
			}

			lvyouSpecialtyProduct.setSellNumber(0);
			if (lvyouSpecialtyProduct.getRank() == null) {
				lvyouSpecialtyProduct.setRank(Integer.MAX_VALUE);
			}
			return lvyouSpecialtyProductDao.save(lvyouSpecialtyProduct);
		}		
	
		
	}
	
	@Override
	public int update(LvyouSpecialtyProduct lvyouSpecialtyProduct){
		return lvyouSpecialtyProductDao.update(lvyouSpecialtyProduct);
	}
	
	@Override
	public int remove(Long id){
		return lvyouSpecialtyProductDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return lvyouSpecialtyProductDao.batchRemove(ids);
	}

	@Override
	public List<LvyouSpecialtyProduct> getByTypeFront(Map<String, Object> param) {
		return lvyouSpecialtyProductDao.getByTypeFront(param);
	}

	@Override
	public List<LvyouSpecialtyProduct> compoundQuery(Map<String, Object> map) {
		return lvyouSpecialtyProductDao.compoundQuery(map);
	}

	@Override
	public Integer compoundQueryCount(Map<String, Object> map) {
		return lvyouSpecialtyProductDao.compoundQueryCount(map);
	}

	@Override
	public List<LvyouSpecialtyProduct> getFromMap(Set<String> ids) {
		return lvyouSpecialtyProductDao.getFromMap(ids);
	}

	@Override
	public List<LvyouSpecialtyProduct> getLunbo(Long typeId) {
		return lvyouSpecialtyProductDao.getLunbo(typeId);
	}


	@Transactional
	@Override
	public int addToLunbo(LvyouSpecialtyProduct lvyouSpecialtyProduct, LvyouSpecialtyLunbo lvyouSpecialtyLunbo) {


		lvyouSpecialtyLunboDao.save(lvyouSpecialtyLunbo);
		lvyouSpecialtyProduct.setLunboId(lvyouSpecialtyLunbo.getId());
		return lvyouSpecialtyProductDao.update(lvyouSpecialtyProduct);
	}

	@Override
	public int deleteFromLunbo(LvyouSpecialtyProduct lvyouSpecialtyProduct) {

		if (lvyouSpecialtyProduct.getLunboId() != null && lvyouSpecialtyProduct.getLunboId() != 0) {
			lvyouSpecialtyLunboDao.remove(lvyouSpecialtyProduct.getLunboId());
			lvyouSpecialtyProduct.setLunboId(0L);
			return lvyouSpecialtyProductDao.update(lvyouSpecialtyProduct);
		}
		return 0;
	}
}
